package org.finesys.common.file.client.oss.http;

import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import lombok.AllArgsConstructor;
import lombok.Cleanup;
import lombok.SneakyThrows;
import org.finesys.common.file.client.oss.service.OssFileTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/oss")
@ConditionalOnProperty(name = "file.oss.info", havingValue = "true")
public class OssEndPoint {

    private final OssFileTemplate ossFileTemplate;

    /**
     * 创建bucketName
     */
    @SneakyThrows
    @PostMapping("/bucket/{bucketName}")
    public Bucket createBucket(@PathVariable String bucketName) {
        ossFileTemplate.createBucket(bucketName);
        return ossFileTemplate.getBucket(bucketName).get();
    }

    /**
     * 获取所有bucketName
     */
    @SneakyThrows
    @GetMapping("/bucket")
    public List<Bucket> getBuckets() {
        return ossFileTemplate.getAllBuckets();
    }

    /**
     * 获取 bucketName
     */
    @SneakyThrows
    @GetMapping("/bucket/{bucketName}")
    public Bucket getBucket(@PathVariable String bucketName) {
        return ossFileTemplate.getBucket(bucketName).orElseThrow(() -> new IllegalArgumentException("BucketName not found"));
    }

    /**
     * 删除 bucketName
     */
    @SneakyThrows

    @DeleteMapping("/bucket/{bucketName}")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public void deleteBucket(@PathVariable String bucketName) {
        ossFileTemplate.removeBucket(bucketName);
    }

    /**
     * 对bucketName下创建文件
     */
    @SneakyThrows
    @PostMapping("/object/{bucketName}")
    public S3Object createObject(@PathVariable String bucketName, @RequestBody MultipartFile multipartFile) {
        String fileName = multipartFile.getOriginalFilename();
        //自动化资源管理器，无需手动关闭资源
        @Cleanup InputStream inputStream = multipartFile.getInputStream();
        ossFileTemplate.putObject(bucketName, fileName, inputStream, multipartFile.getContentType());
        return ossFileTemplate.getObject(bucketName, fileName);
    }

    /**
     * 对bucketName下创建文件
     */
    @SneakyThrows
    @PostMapping("/object/{bucketName}/{objectName}")
    public S3Object createObject(@PathVariable String bucketName, @PathVariable String objectName, @RequestBody MultipartFile multipartFile) {
        //自动化资源管理器，无需手动关闭资源
        @Cleanup InputStream inputStream = multipartFile.getInputStream();
        ossFileTemplate.putObject(bucketName, objectName, inputStream, multipartFile.getContentType());
        return ossFileTemplate.getObject(bucketName, objectName);
    }

    /**
     * 获取bucketName下前缀是objectName的文件
     */
    @SneakyThrows
    @GetMapping("/object/{bucketName}/{objectName}")
    public List<S3ObjectSummary> filterObject(@PathVariable String bucketName, @PathVariable String objectName) {
        return ossFileTemplate.getAllObjectsByPrefix(bucketName, objectName, true);
    }


    /**
     * 获取bucketName下前缀是objectName的文件
     */
    @SneakyThrows
    @GetMapping("/object/{bucketName}/{objectName}/{expires}")
    public Map<String, Object> getObject(@PathVariable String bucketName, @PathVariable String objectName, @PathVariable Integer expires) {
        Map<String, Object> map = new HashMap<>();
        map.put("bucketName", bucketName);
        map.put("objectName", objectName);
        map.put("expires", expires);
        map.put("url", ossFileTemplate.getObjectUrl(bucketName, objectName, expires));
        return map;
    }

    /**
     * 删除 bucketName 下 ObjectName 文件
     */
    @SneakyThrows
    @DeleteMapping("/bucket/{bucketName}/{objectName}")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public void deleteObject(@PathVariable String bucketName, @PathVariable String objectName) {
        ossFileTemplate.removeObject(bucketName, objectName);
    }
}
